package components
{
	import flash.desktop.ClipboardFormats;
	import flash.desktop.NativeDragActions;
	import flash.desktop.NativeDragManager;
	import flash.display.InteractiveObject;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class EditResource{
		private var panel:SuperPanel = new SuperPanel();
		private var image:Image = new Image();
		private var tool:Tool = new Tool();
		
		private var fileNameLabel:Label = new Label();
		private var fileNameTextField:TextArea = new TextArea();
		private var sifraLabel:Label = new Label();
		private var sifraTextField:TextInput = new TextInput();
		private var resursNameLabel:Label = new Label();
		private var resursNameTextField:TextInput = new TextInput();
		private var opisLabel:Label = new Label();
		private var opisTextField:TextInput = new TextInput();
		private var posalji:Button = new Button();
		
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var service2:Mysql = new Mysql();
		
		private var dropfile:File;
		
		private var f:FileReference;
		
		private var newFileBtn:Button = new Button();
		private var newWebBtn:Button = new Button();
		private var newTextBtn:Button = new Button();
		
		private var _id:String;
		private var _tip:String;
		private var _nazivFajla:String;
		private var _sifra:String;
		private var _nazivResursa:String;
		private var _opis:String;
		
		public function EditResource(p:SuperPanel){
			panel = p;
		}
		
		public function show(id:String, tip:String, nazivFajla:String, sifra:String, nazivResursa:String, opis:String):void{
			_id = id;
			_tip = tip;
			_nazivFajla = nazivFajla;
			_sifra = sifra;
			_nazivResursa = nazivResursa;
			_opis = opis;
			if(_tip=="html") editWeb(); else
			if(_tip=="slt") editText(); else editFile();
		}
			
		public function editFile():void{
			image.x = panel.width - 122;
			image.y = 43;
			image.height = 100;
	 		image.width=100;
	 		image.source = panel.parentApplication.AVATAR_PATH+"draghere.jpg";

	 		fileNameLabel.x = 10;
	 		fileNameLabel.y = 27;
	 		fileNameLabel.width = panel.width - 140;
	 		fileNameLabel.text = "*Naziv fajla: ";
	 		fileNameTextField.x = 10;
	 		fileNameTextField.y = 43;
	 		fileNameTextField.width = panel.width - 140;
	 		fileNameTextField.editable = false;
	 		fileNameTextField.height = 20;
	 		fileNameTextField.text = _nazivFajla;
			
			sifraLabel.x = 10;
	 		sifraLabel.y = 67;
	 		sifraLabel.width = panel.width - 140;
	 		sifraLabel.text = "Sifra (prazno=javno): ";
	 		sifraTextField.x = 10;
	 		sifraTextField.y = 83;
	 		sifraTextField.width = panel.width - 140;
	 		sifraTextField.displayAsPassword = true;
	 		sifraTextField.text = _sifra;
	 		
	 		resursNameLabel.x = 10;
	 		resursNameLabel.y = 107;
	 		resursNameLabel.width = panel.width - 140;
	 		resursNameLabel.text = "*Naziv resursa: ";
	 		resursNameTextField.x = 10;
	 		resursNameTextField.y = 123;
	 		resursNameTextField.width = panel.width - 140;
	 		resursNameTextField.text = _nazivResursa;
	 		
	 		opisLabel.x = 10;
	 		opisLabel.y = 147;
	 		opisLabel.width = panel.width - 35;
	 		opisLabel.text = "*Opis resursa: ";
	 		opisTextField.x = 10;
	 		opisTextField.y = 163;
	 		opisTextField.width = panel.width - 35;
	 		opisTextField.height = 40;
	 		opisTextField.text = _opis;
	 		
	 		posalji.x = 10;
	 		posalji.y = 210;
	 		posalji.width = panel.width - 35;
	 		posalji.label = "Dodaj resurs";
			
			panel.addChild(image);
			panel.addChild(fileNameTextField);
			panel.addChild(fileNameLabel);
			panel.addChild(resursNameLabel);
			panel.addChild(resursNameTextField);
			panel.addChild(opisLabel);
			panel.addChild(opisTextField);
			panel.addChild(sifraTextField);
			panel.addChild(sifraLabel);
			panel.addChild(posalji);
			image.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, acceptDrag);
			image.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, dragDrop);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			posalji.addEventListener(MouseEvent.CLICK, onPosalji);
		}
		
		public function editWeb():void{
			sifraLabel.x = 10;
	 		sifraLabel.y = 67;
	 		sifraLabel.width = panel.width - 140;
	 		sifraLabel.text = "Sifra (prazno=javno): ";
	 		sifraTextField.x = 10;
	 		sifraTextField.y = 83;
	 		sifraTextField.width = panel.width - 35;
	 		sifraTextField.displayAsPassword = true;
	 		sifraTextField.text = _sifra;	
	 		
	 		resursNameLabel.x = 10;
	 		resursNameLabel.y = 27;
	 		resursNameLabel.width = panel.width - 35;
	 		resursNameLabel.text = "*Naziv resursa: ";
	 		resursNameTextField.x = 10;
	 		resursNameTextField.y = 43;
	 		resursNameTextField.width = panel.width - 35;
	 		resursNameTextField.text = _nazivResursa;		
	 		
	 		opisLabel.x = 10;
	 		opisLabel.y = 167;
	 		opisLabel.width = panel.width - 35;
	 		opisLabel.text = "*Opis resursa: ";
	 		opisTextField.x = 10;
	 		opisTextField.y = 183;
	 		opisTextField.width = panel.width - 35;
	 		opisTextField.text = _opis;		
	 		
	 		fileNameLabel.x = 10;
	 		fileNameLabel.y = 107;
	 		fileNameLabel.width = panel.width - 35;
	 		fileNameLabel.text = "*Link: ";
	 		fileNameTextField.x = 10;
	 		fileNameTextField.y = 123;
	 		fileNameTextField.width = panel.width - 35;
	 		fileNameTextField.height = 40;
	 		fileNameTextField.text = _nazivFajla;	
	 		
	 		posalji.x = 10;
	 		posalji.y = 210;
	 		posalji.width = panel.width - 35;
	 		posalji.label = "Dodaj resurs";
			
			panel.addChild(fileNameLabel);
			panel.addChild(fileNameTextField);
			panel.addChild(resursNameLabel);
			panel.addChild(resursNameTextField);
			panel.addChild(opisLabel);
			panel.addChild(opisTextField);
			panel.addChild(sifraTextField);
			panel.addChild(sifraLabel);
			panel.addChild(posalji);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			posalji.addEventListener(MouseEvent.CLICK, onPosaljiWeb);
		}
		
		public function editText():void{
			sifraLabel.x = 10;
	 		sifraLabel.y = 67;
	 		sifraLabel.width = panel.width - 140;
	 		sifraLabel.text = "Sifra (prazno=javno): ";
	 		sifraTextField.x = 10;
	 		sifraTextField.y = 83;
	 		sifraTextField.width = panel.width - 35;
	 		sifraTextField.displayAsPassword = true;
	 		sifraTextField.text = _sifra;		
	 		
	 		resursNameLabel.x = 10;
	 		resursNameLabel.y = 27;
	 		resursNameLabel.width = panel.width - 35;
	 		resursNameLabel.text = "*Naziv resursa: ";
	 		resursNameTextField.x = 10;
	 		resursNameTextField.y = 43;
	 		resursNameTextField.width = panel.width - 35;
	 		resursNameTextField.text = _nazivResursa;	
	 		
	 		opisLabel.x = 10;
	 		opisLabel.y = 167;
	 		opisLabel.width = panel.width - 35;
	 		opisLabel.text = "*Opis resursa: ";
	 		opisTextField.x = 10;
	 		opisTextField.y = 183;
	 		opisTextField.width = panel.width - 35;
	 		opisTextField.text = _opis;		
	 		
	 		fileNameLabel.x = 10;
	 		fileNameLabel.y = 107;
	 		fileNameLabel.width = panel.width - 35;
	 		fileNameLabel.text = "*Tekst: ";
	 		fileNameTextField.x = 10;
	 		fileNameTextField.y = 123;
	 		fileNameTextField.width = panel.width - 35;
	 		fileNameTextField.height = 40;
	 		fileNameTextField.text = _nazivFajla;	
	 		
	 		posalji.x = 10;
	 		posalji.y = 210;
	 		posalji.width = panel.width - 35;
	 		posalji.label = "Obnovi resurs";
			
			panel.addChild(fileNameLabel);
			panel.addChild(fileNameTextField);
			panel.addChild(resursNameLabel);
			panel.addChild(resursNameTextField);
			panel.addChild(opisLabel);
			panel.addChild(opisTextField);
			panel.addChild(sifraTextField);
			panel.addChild(sifraLabel);
			panel.addChild(posalji);
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			posalji.addEventListener(MouseEvent.CLICK, onPosaljiText);
		}
		
		public function onPosaljiWeb(e:MouseEvent):void{
			if(fileNameTextField.text.length > 0 && resursNameTextField.text.length > 0 && opisTextField.text.length > 0){
				service1.sql = {type:"UPDATE", tables:"resursi r", s1:"r.url=@"+fileNameTextField.text+"@", s2:"r.sifra=@"+sifraTextField.text+"@", s3:"r.naziv=@"+resursNameTextField.text+"@", s4:"r.opis=@"+opisTextField.text+"@", where:"r.id_resurs=@"+_id+"@"};
				service1.addEventListener(service1.MYSQL_RESULT, onPosaljiFinish);
				service1.startService();
			} else tool.createAlert("GRESKA", "Niste unijeli sve podatke!");
		}
		
		public function onPosaljiText(e:MouseEvent):void{
			if(fileNameTextField.text.length > 0 && resursNameTextField.text.length > 0 && opisTextField.text.length > 0){
				service1.sql = {type:"UPDATE", tables:"resursi r", s1:"r.url=@"+fileNameTextField.text+"@", s2:"r.sifra=@"+sifraTextField.text+"@", s3:"r.naziv=@"+resursNameTextField.text+"@", s4:"r.opis=@"+opisTextField.text+"@", where:"r.id_resurs=@"+_id+"@"};
				service1.addEventListener(service1.MYSQL_RESULT, onPosaljiFinish);
				service1.startService();
			} else tool.createAlert("GRESKA", "Niste unijeli sve podatke!");
		}
		
		public function onPosaljiFinish(e:Event):void{
			tool.createAlert("OK", "Resurs je obnovljen!");
			panel.closePanel();
		}
		
		public function onPosalji(e:MouseEvent):void{
			if(fileNameTextField.text.length > 0 && resursNameTextField.text.length > 0 && opisTextField.text.length > 0){
				if(dropfile){
					service.sql = {type:"SELECT", a1:"t.id_tip", from:"tipovi t", where:"t.tip=@"+dropfile.extension.toLocaleLowerCase()+"@" };
				} else service.sql = {type:"SELECT", a1:"t.id_tip", from:"tipovi t", where:"t.tip=@"+_tip+"@" };
				service.addEventListener(service.MYSQL_RESULT, onCheckedFileType);
				service.startService();
			} else tool.createAlert("GRESKA", "Niste unijeli sve podatke!");
		}
		
		public function onCheckedFileType(e:Event):void{
			var xmlList:XMLList = service.xml.children();
			if(xmlList.length()>0){
				if(!dropfile){
					service1.sql = {type:"UPDATE", tables:"resursi r", s1:"r.url=@"+fileNameTextField.text+"@", s2:"r.sifra=@"+sifraTextField.text+"@", s3:"r.naziv=@"+resursNameTextField.text+"@", s4:"r.opis=@"+opisTextField.text+"@", s5:"r.tip=@"+xmlList[0].id_tip+"@", where:"r.id_resurs=@"+_id+"@"};
					service1.addEventListener(service1.MYSQL_RESULT, onUpload1);
				} else {
					service1.sql = {type:"UPDATE", tables:"resursi r", s1:"r.url=@"+dropfile.name+"@", s2:"r.sifra=@"+sifraTextField.text+"@", s3:"r.naziv=@"+resursNameTextField.text+"@", s4:"r.opis=@"+opisTextField.text+"@", s5:"r.tip=@"+xmlList[0].id_tip+"@", where:"r.id_resurs=@"+_id+"@"};
					service1.addEventListener(service1.MYSQL_RESULT, onUpload);
				}
				service1.startService();
			} else tool.createAlert("GRESKA", "Tip fajla nije prihvatljiv!");
		}
		
		public function onUpload(e:Event):void{	
			tool.createAlert("OK", "Resurs je obnovljen, ali mozda ne i uploadovan.\nOstavite prozor otvoren dok ne dobijete potvrdu da je upload zavrsen!");
			f.addEventListener(Event.COMPLETE, completeHandler);
			f.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			f.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
   			var request:URLRequest = new URLRequest(panel.parentApplication.UPLOAD_MATERIJAL_SERVICE)
   			f.upload(request);
		}
		
		public function onUpload1(e:Event):void{	
			tool.createAlert("OK", "Resurs je obnovljen");
			panel.closePanel(); 	
		}
		
		public function ioErrorHandler(event:Event):void{
    		tool.createAlert("GRESKA", "Dogodila se greska prilikom uploada!");
		}	
			
		public function httpStatusHandler(event:HTTPStatusEvent):void {
            trace("httpStatusHandler: " + event);
        }
        
		public function completeHandler(event:Event):void{
    		service2.sql = {type:"DELETEMATERIJAL", file:_nazivFajla };
	 		service2.addEventListener(service1.MYSQL_RESULT, onDeleteMaterijalResult);
			service2.startService();
    		
    		tool.createAlert("OK", "Fajl je uspjesno uploadovan!");
    		dropfile.deleteFile();
 			panel.closePanel(); 			
		}	
		
		public function acceptDrag(event:NativeDragEvent):void {
			NativeDragManager.dropAction = NativeDragActions.MOVE;
        	NativeDragManager.acceptDragDrop(event.currentTarget as InteractiveObject);
		}
		
		public function dragDrop(event:NativeDragEvent):void { 
			var temp:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			for each (var file:File in temp){
				var dodaj:String = tool.uniqueNumber();
				var destination:File;
				destination = File.documentsDirectory.resolvePath(dodaj+"."+file.extension);
				file.copyTo(destination, true);
				fileNameTextField.text = file.name;
				file = destination;
				if(dropfile) dropfile.deleteFile();
				dropfile = file;
				f = FileReference(file);
			}
		}
		
		public function onResize(e:ResizeEvent):void{

		}
		
		public function onDeleteMaterijalResult(e:Event):void{
		}
	}
}